package com.siblings.loan.ui.loan.act

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.View
import com.lib.base.base.BaseActivity
import com.lib.base.utils.thread.TimingThread
import com.siblings.loan.R
import com.siblings.loan.databinding.ActLoanApplyWaitBinding
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.utils.dialog.KefuDialog
import com.siblings.loan.utils.event.SibEventType
import kotlin.random.Random

/**
 * SiblingsLoan
 * @Author wwdeng
 * @CreateTime: 2024/7/12 18:45
 * @Annotation：申请贷款等待
 */
class SibLoanApplyWaitAct: BaseActivity(),TimingThread.ITimingThreadListener {
    lateinit var binding: ActLoanApplyWaitBinding
    private var timeThread: TimingThread? = null
    //等待时长
    private var countdown = 10

    companion object {
        private const val INTENT_KEY_AMOUNT = "amount"
        private const val INTENT_KEY_DAYS = "days"
        fun start(context: Context, amount: String,days: String) {
            val intent = Intent(context, SibLoanApplyWaitAct::class.java)
            intent.putExtra(INTENT_KEY_AMOUNT, amount)
            intent.putExtra(INTENT_KEY_DAYS, days)
            context.startActivity(intent)
        }
    }

    override fun getSibRootView(): View {
        binding = ActLoanApplyWaitBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initSibView() {
        setSibTitleBarColor(Color.parseColor("#FFFFFFFF"))
        setSibTextTitle("")
        setSibBackBtnVisibility(View.INVISIBLE)
        setSibRightBtnRes(R.mipmap.pic_kefu_theme)
    }

    override fun initSibListener() {
        //联系客服
        rootViewBinding.ivFun.setOnClickListener {
            showSibKefuDialog("Loan Apply Wait Page")
            HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_CUSTOMER, "Loan Apply Wait Page")
        }
    }

    override fun initSibData() {
        startTimeThread()
    }

    /**
     * 启动倒计时
     */
    private fun startTimeThread() {
        if (timeThread != null) {
            timeThread!!.stopThread()
        }
        //随机 5~10秒 等待时长
        countdown = 5 + Random.nextInt(4)
        showSibTipsText()
        if (timeThread == null) {
            timeThread = TimingThread()
            timeThread!!.setThreadDelayMillis(1000)
            timeThread!!.setLoadListener(this)
        }
        timeThread!!.startThread()
    }

    override fun onLoading() {
        if (countdown <= 0){
            countdown = 10
            timeThread!!.stopThread()
            showSibLoanApplySuccess()
            return
        }
        countdown--
        showSibTipsText()
    }

    /**
     * 设置文本
     */
    private fun showSibTipsText(){
        val textSMS = getString(R.string.loan_apply_wait_sms)
        val showText = String.format(getString(R.string.loan_apply_wait_tips),textSMS,"$countdown")

        val smsIndex = showText.indexOf(textSMS)
        val spString = SpannableString(showText)
        spString.setSpan(ForegroundColorSpan(Color.parseColor("#FFFF0000")), smsIndex, smsIndex + textSMS.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spString.setSpan(StyleSpan(Typeface.BOLD), smsIndex, smsIndex + textSMS.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        binding.textWaitTips.text = spString
    }

    /**
     * 显示客服dialog
     */
    private fun showSibKefuDialog(eventRemark: String){
        val kefuDialog = KefuDialog(this,eventRemark,object : KefuDialog.IDialogCallback{
            override fun onClick(view: View?) {
            }
        })
        kefuDialog.setCancelable(true)
        kefuDialog.setCanceledOnTouchOutside(true)
        kefuDialog.show()
    }

    /**
     * 贷款订单创建成功
     */
    private fun showSibLoanApplySuccess(){
        //返回首页
        finish()
    }

    /**
     * 获取贷款金额
     */
    private fun getSibApplyAmount(): String? {
        return intent.extras!!.getString(INTENT_KEY_AMOUNT)
    }

    /**
     * 获取贷款时长
     */
    private fun getSibApplyDays(): String? {
        return intent.extras!!.getString(INTENT_KEY_DAYS)
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_LOAN_APPLY_WAIT_START, "")
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_LOAN_APPLY_WAIT_END, "")
        if (timeThread != null) {
            timeThread!!.stopThread()
            timeThread = null
        }
    }
}